package com.kusiri.kframework
{
	
	import com.kusiri.kframework.locale.ILocaleSettings;
	import com.kusiri.kframework.locale.LocaleSettings;
	import com.kusiri.kframework.ui.*;
	import com.kusiri.kframework.uibuilder.UiBuilder;
	
	import mx.core.IContainer;
	import mx.logging.Log;
	import mx.logging.targets.TraceTarget;
	import mx.managers.CursorManager;
	
	/**
	 * The main application class.
	 */
	public class KfApplication implements IKfApplication
	{

		public static const UI_COMPONENT_INTERFACE : String = 'com.kusiri.kframework.ui::IKfUiComponent';
		public static const DATA_COMPONENT_INTERFACE : String  = 'com.kusiri.kframework.model::IKfDataComponent';
		public static const SERVICE_COMPONENT_INTERFACE : String  = 'com.kusiri.kframework.service::IKfServiceComponent';

		protected var self : KfApplication;

		/**
		 * Instance vars/functions
		 */
		
		protected var _context : KfContext;
		
		/**
		 * What the UI container is contained by
		 */
		protected var _uiContainerParent:IContainer;
		
		/**
		 * The UI container
		 */
		protected var _uiContainer : IKfUiContainer;
		
		/**
		 * 
		 */
		protected var _localeSettings : LocaleSettings;


		/**
		 * 
		 */
		public function KfApplication() : void
		{
			self = this;
			
			_setupLogging();
			
			KfApplicationLocator._kfApplication = this;
		}
		
		private function _setupLogging() : void
		{
			var target : TraceTarget = new TraceTarget();
		    target.includeCategory=true
		    target.includeDate=true
		    target.includeLevel=true
		    target.includeTime=true
		    target.level=0;
		    //target.filters = ['mx.rpc.*','mx.messaging.*'];
      
			Log.addTarget(target);
		}
		
		/**
		 * 
		 */
		public function get uiContainer() : IKfUiContainer
		{
			return _uiContainer;
		}
		
		
		/**
		 * 
		 */
		public function get context() : IKfContext
		{
			return _context;
		}
		
		/**
		 * Global date formatter
		 */
		public function get localeSettings() : ILocaleSettings
		{
			return _localeSettings;
		}
		
		/**
		 * Called to kick off setting up the application
		 */
		public function initialize( config:XML, callback : Function ) : void
		{
			// TODO: Work out locale stuff
			_localeSettings = new LocaleSettings();
			
			_context = new KfContext();
						
			_context.initialize(config, 
				function() : void
				{ 
					callback(self);
				} );	
		} 

		/**
		 * Let's go!
		 */
		public function startApplication(uiContainerParent : IContainer) : void
		{
			_uiContainerParent = uiContainerParent;
			
			_uiContainer = _context.uiContainer;
			_uiContainer.initialize(_uiContainerParent, _initializeUi);
		}	
		
		/**
		 * 
		 */
		public function startUiBuilder(container : IContainer) : void
		{
			var builder : UiBuilder = new UiBuilder();
			builder.context = _context;
			container.addChild(builder);
		}
		
		/**
		 * Initialises the UI
		 */
		private function _initializeUi(e:*=null) : void
		{
			_context.initializeInitialUi();
		}
		
		/**
		 * Called when we are waiting for something
		 */
		public function waitingForSomething() : void
		{
			CursorManager.setBusyCursor();
		}
		
		/**
		 * Called when we are waiting for something
		 */
		public function finishedWaitingForSomething() : void
		{
			CursorManager.removeBusyCursor();
		}
		
	}
}